/* This program creates a stacked dataset for the school district capitalization wind project. For each year a set of wind installations becomes operational,
we keep all the school districts associated with those projects plus all the school districts that never had a wind installation. We then stack these project
cohorts to create our final dataset. */


log using results, replace

	global Data "R:\CLAS_Brunner\District_Capitalization\Data"
	global Results "R:\CLAS_Brunner\District_Capitalization\Results"
	global Index "R:\CLAS_Brunner\District_Capitalization\Data\Housing Index"
	

use "$Data\district_wind_dataset"
merge 1:1 ncesid year using "$Index\school_year_collapse_over2_updated"
* merge 1:1 ncesid year using "$Index\school_year_collapse_over1_half_updated"
* merge 1:1 ncesid year using "$Index\school_year_collapse_over1_updated"
* merge 1:1 ncesid year using "$Index\school_year_collapse_updated"

drop if _merge~=3
drop _merge

* Housing data ends in 2021 so set p_year equal to missing for 2022

replace min_p_year=. if min_p_year==2022


save temp_data, replace

keep if min_p_year==2005 | min_p_year==.
gen cohort=1
save "$Data\cohort1", replace

use temp_data, clear
keep if min_p_year==2006 | min_p_year==.
gen cohort=2
save "$Data\cohort2", replace

use temp_data, clear
keep if min_p_year==2007 | min_p_year==.
gen cohort=3
save "$Data\cohort3", replace

use temp_data, clear
keep if min_p_year==2008 | min_p_year==.
gen cohort=4
save "$Data\cohort4", replace

use temp_data, clear
keep if min_p_year==2009 | min_p_year==.
gen cohort=5
save "$Data\cohort5", replace

use temp_data, clear
keep if min_p_year==2010 | min_p_year==.
gen cohort=6
save "$Data\cohort6", replace

use temp_data, clear
keep if min_p_year==2011 | min_p_year==.
gen cohort=7
save "$Data\cohort7", replace

use temp_data, clear
keep if min_p_year==2012 | min_p_year==.
gen cohort=8
save "$Data\cohort8", replace

use temp_data, clear
keep if min_p_year==2013 | min_p_year==.
gen cohort=9
save "$Data\cohort9", replace

use temp_data, clear
keep if min_p_year==2014 | min_p_year==.
gen cohort=10
save "$Data\cohort10", replace

use temp_data, clear
keep if min_p_year==2015 | min_p_year==.
gen cohort=11
save "$Data\cohort11", replace

use temp_data, clear
keep if min_p_year==2016 | min_p_year==.
gen cohort=12
save "$Data\cohort12", replace

use temp_data, clear
keep if min_p_year==2017 | min_p_year==.
gen cohort=13
save "$Data\cohort13", replace

use temp_data, clear
keep if min_p_year==2018 | min_p_year==.
gen cohort=14
save "$Data\cohort14", replace

use temp_data, clear
keep if min_p_year==2019 | min_p_year==.
gen cohort=15
save "$Data\cohort15", replace

use temp_data, clear
keep if min_p_year==2020 | min_p_year==.
gen cohort=16
save "$Data\cohort16", replace

use temp_data, clear
keep if min_p_year==2021 | min_p_year==.
gen cohort=17
save "$Data\cohort17", replace

use "$Data\cohort1", clear
append using "$Data\cohort2" "$Data\cohort3" "$Data\cohort4" "$Data\cohort5" "$Data\cohort6" "$Data\cohort7" ///
"$Data\cohort8" "$Data\cohort9" "$Data\cohort10" "$Data\cohort11" "$Data\cohort12" "$Data\cohort13" ///
"$Data\cohort14" "$Data\cohort15" "$Data\cohort16" "$Data\cohort17"

compress
save "$Data\stacked_data_aug23", replace
